/**
 * 关键词回复Model
 * @authors 杨兴洲（of2502）
 * @date    2017/3/21 9:49
 * @version 1.0
 */

import {model, Schema} from "mongoose";
const KeywordReplySchema = new Schema({
    appID: String,
    // rule: {"$type": Schema.Types.ObjectId, ref: "Reply"},
    rule_name: String,
    reply_mode: {
        "$type": String,
        "enum": ["random_one", "reply_all"]
    },
    keyword_list_info: [{
        type: {
            "$type": String,
            "enum": ["text", "image", "voice", "video", "shortvideo", "location", "link"]//目前只处理text类型(关键词回复)
        },
        match_mode: {
            "$type": String,
            "enum": ["contain", "equal"]
        },
        content: String,
        _id: false
    }],
    reply_list_info: [{
        type: {
            "$type": String,
            "enum": ["text", "image", "voice", "video", "music", "news", "mpnews", "wxcard"]
        },
        content: String,
        url: String,// 视频/音频/图片的url.目前只有图片
        // 视频和音乐的额外信息
        title: String,
        description: String,
        thumb_media_id: String,
        musicurl: String,
        hqmusicurl: String,
        _id: false
    }],
}, {
    timestamps: true,
    typeKey: "$type",
    id: false,
    toJSON: {virtuals: true}
});

KeywordReplySchema.pre('remove', function (next) {
    const project = this;
    project.model('Reply').update(
        {projects: {$in: project.keyword_autoreply_info.list}},
        {$pull: {project: project._id}},
        {multi: true},
        next
    );
});
KeywordReplySchema.virtual("ruleID").get(function () {
    return this._id;
});
KeywordReplySchema.index({"appID": 1, "keyword_list_info.content": 1});
KeywordReplySchema.index({"keyword_list_info.content": "text"});

const KeywordReply = model("KeywordReply", KeywordReplySchema);
export default KeywordReply;
